package com.lee.blepwm.Contant;

import com.blankj.utilcode.util.ConvertUtils;

import org.junit.Test;

import java.math.BigInteger;
import java.util.UUID;

/**
 * declaration:
 * time:
 */
public class Instructions {

    public static UUID SERVICE_UUID = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb");
    public static UUID NOTIFY_UUID = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");
    public static UUID CONFIG_UUID = UUID.fromString("0000ffe2-0000-1000-8000-00805f9b34fb");
    public static UUID WRITE_UUID = UUID.fromString("0000ffe3-0000-1000-8000-00805f9b34fb");

    /**
     * 设置50HZ
     */
    public static byte[] SETTING_50_HZ = ConvertUtils.hexString2Bytes("E8A20032");
    /**
     * 设置频率
     */
    public static String SETTING_HZ = "E8A2";
    /**
     * 开启PWM
     */
    public static byte[] SETTING_START_PWM = ConvertUtils.hexString2Bytes("E8A101");
    /**
     * 关闭PWM
     */
    public static byte[] SETTING_STOP_PWM = ConvertUtils.hexString2Bytes("E8A100");
    /**
     * 断开连接
     */
    public static byte[] SETTING_DISCONNECTION = ConvertUtils.hexString2Bytes("E90102");

    /**
     * 设置PWM1 占空比前置参数
     */
    public static String SETTING_PWM1_PREFIX = "E8A3";
    /**
     * 设置PWM2 占空比前置参数
     */
    public static String SETTING_PWM2_PREFIX = "E8A4";
    /**
     * 设置PWM3 占空比前置参数
     */
    public static String SETTING_PWM3_PREFIX = "E8A5";
    /**
     * 设置PWM4 占空比前置参数
     */
    public static String SETTING_PWM4_PREFIX = "E8A6";

    /**
     * 读 PWM 开关状态
     * A831 表示 PWM 打开状态
     * A830 表示 PWM 关闭状态
     */
    public static byte[] READ_PWM_STATUS = ConvertUtils.hexString2Bytes("E8A8");
    /**
     * 读 PWM 频率
     * A903E8 表示频率为 1000HZ
     */
    public static byte[] READ_PWM_FREQUENCY = ConvertUtils.hexString2Bytes("E8A9");
    /**
     * 读 PWM1 占空比
     */
    public static byte[] READ_PWM1_NUM = ConvertUtils.hexString2Bytes("E8AA");
    /**
     * 读 PWM1 占空比
     */
    public static byte[] READ_PWM2_NUM = ConvertUtils.hexString2Bytes("E8AB");
    /**
     * 读 PWM1 占空比
     */
    public static byte[] READ_PWM3_NUM = ConvertUtils.hexString2Bytes("E8AC");
    /**
     * 读 PWM1 占空比
     */
    public static byte[] READ_PWM4_NUM = ConvertUtils.hexString2Bytes("E8AD");

    /**
     * 获取设置PWM占空比的Hex数值
     *
     * @param prefix     需要设置的PWM
     * @param percentage 设置的百分比
     * @return
     */
    public static String getInstruction2Hex(String prefix, int percentage) {
        int s = (int) (percentage / 100F * 255F);
        String str = prefix + ConvertUtils.int2HexString(s).toUpperCase();
        return ConvertUtils.bytes2HexString(str.getBytes());
    }

    /**
     * 获取设置PWM占空比
     *
     * @param hexString
     * @return
     */
    public static float getInstruction(String hexString) {
        String str = new String(ConvertUtils.hexString2Bytes(hexString));
        String substring = str.substring(2);
        BigInteger bigInteger = new BigInteger(substring, 16);
        return bigInteger.floatValue() / 255F * 100;
    }

    /**
     * 获取设置PWM频率的Hex数值
     *
     * @param num 数值 50
     * @return
     */
    public static String getPWMFrequency2Hex(int num) {
        String str = SETTING_HZ + String.format("%04X", num);
        return ConvertUtils.bytes2HexString(str.getBytes());
    }

    @Test
    public void aa() {
//        System.out.println(getInstruction2Hex(SETTING_PWM1_PREFIX, 90));
//        System.out.println(getInstruction("41334535"));

//        String vs = String.format("%02x", 200);
////        byte[] bytes = hex2byte("E8A8".getBytes());
//
//        byte[] bytes = ConvertUtils.hexString2Bytes("E8A8");
//        for (int i = 0; i < bytes.length; i++) {
//            System.out.println(bytes[i]);
//        }

//        for (int i = 0; i <= 25; i++) {
//            int progress = i;
//            float s = progress / 25F * 100F;
//            String vs = String.format("E8A3%02x", progress);
////        0d--19
//            System.out.println(vs);
//            System.out.println(s);
//        }

//        BigInteger bigInteger = new BigInteger("E6",16);
//        System.out.println(bigInteger.intValue()/255F);

        // y = a+bx

        /**
         * y = 153
         * a= 12
         * b= 25
         */

    }

}
